home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 5 / Amiga Tools 5.iso / grafik / 3d & render tools / irit / contrib / wavefrnt / wf2irit.c < prev   
Encoding:
C/C++ Source or Header  |  1996-07-16  |  5.2 KB  |  253 lines

  1. #include "string.h"
  2. #include "stdio.h"
  3. #include "malloc.h"
  4. #include "stdlib.h" 
  5.  
  6.  
  7. typedef struct GROUP_TAG {
  8.     char name[100];
  9.     int id;
  10.     int fnum;
  11.     int FirstFace;
  12. } Group;
  13.  
  14. typedef int VIndex[3];
  15. typedef VIndex *Face;
  16. typedef float Vert[3]; 
  17.  
  18. static int GGnum = 0;
  19. static int GVnum = 0;
  20. static int GVnnum = 0;
  21. static int GFnum = 0;
  22. static int GPrevGroup = 0;
  23. static int GCurrGroup = 0;
  24. static int GCurrGroupOpen = 0;
  25. static Group *GGroups;
  26. static Vert *GVerts;
  27. static Vert *GNverts;
  28.  
  29. static void Pass1(FILE *);
  30. static Vert *Add100(Vert **old, int items);
  31. static int strwrd(char * line);
  32.  
  33. /* HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH */
  34. /* HHHHH     add100           HHHHH */
  35. /* HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH */
  36. static Vert *Add100(Vert **old, int items)
  37. {
  38.     if (*old == NULL)
  39.     *old = (Vert *) malloc((items + 102) * sizeof(Vert));
  40.     else
  41.     *old = (Vert *) realloc((char *) *old, (items + 102) * sizeof(Vert));
  42.  
  43.     if (*old == NULL) {   
  44.     fprintf(stderr, "wf2irit: memory allocation failed\n");
  45.     exit(1);
  46.     }
  47.  
  48.     return *old;
  49. }
  50.  
  51. /* HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH */
  52. /* HHHHH     main             HHHHH */
  53. /* HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH */
  54. main(int argc,char * argv[])
  55. {
  56.     FILE * ifile;
  57.     int i;
  58.     int flag;
  59.     char **DataFileNames;
  60.  
  61.     DataFileNames = argv + 1;
  62.     flag = argc - 1;
  63.  
  64.     for (i = 0; i < argc - 1 || !flag; i++) {
  65.     if (!flag) {
  66.         ifile = stdin;
  67.         flag = !flag;
  68.     }
  69.     else {
  70.         fprintf(stderr, "Processing \"%s\"\n", *DataFileNames);
  71.         if ((ifile = fopen(*DataFileNames, "r")) == NULL) {   
  72.         fprintf(stderr, "Can't open data file %s.\n", *DataFileNames);
  73.         exit(1);
  74.         }
  75.     }
  76.  
  77.     GVerts = (Vert *) malloc(100 * sizeof(Vert));
  78.     GNverts = (Vert *) malloc(100 * sizeof(Vert));
  79.  
  80.     GGnum = 0;
  81.     GVnum = 0;
  82.     GVnnum = 0;
  83.     GFnum = 0;
  84.     GPrevGroup = 0;
  85.     GCurrGroup = 0;
  86.     GCurrGroupOpen = 0;
  87.  
  88.     Pass1(ifile);
  89.  
  90.     free(GGroups);
  91.     free(GVerts);
  92.     free(GNverts);
  93.  
  94.     fclose(ifile);
  95.  
  96.     DataFileNames++;              /* Skip to next file name. */
  97.     }
  98. }
  99.  
  100.  
  101. /* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX */
  102. /* XXXXXXXX             Pass1                          XXXXXXXX */
  103. /* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX */
  104. static void Pass1(FILE *f)
  105. {
  106.     char Line[200];
  107.     char FirstWord[20];
  108.  
  109.     while (!feof(f)) {
  110.     fgets(Line, 100, f);
  111.     if (strlen(Line) < 2)
  112.         continue;
  113.  
  114.     sscanf(Line, "%s", FirstWord);
  115.  
  116.     if (!strcmp(FirstWord,"g")) {
  117.         int i;
  118.         char gname[100];
  119.  
  120.         if(GCurrGroupOpen) {
  121.         fprintf(stdout,"] /* OBJECT %s */\n", 
  122.             GGroups[GCurrGroup].name);
  123.         GCurrGroupOpen = 0;
  124.         }
  125.  
  126.         sscanf(Line, "g %s", gname);
  127.  
  128.         /* Look for the groupname among already found. */
  129.  
  130.         for (i=0; i<GGnum; i++)
  131.             if (!strcmp(GGroups[i].name, gname))
  132.             break;
  133.  
  134.         if (i== GGnum) {
  135.         /* If the found group is new. */
  136.         if (GGroups == NULL)
  137.             GGroups = (Group *) malloc((GGnum +1) * sizeof(Group));
  138.         else
  139.             GGroups = (Group *) realloc((char*)GGroups,
  140.                         (GGnum +1) * sizeof(Group));
  141.  
  142.         if (GGroups == NULL) {   
  143.             fprintf(stderr, "wf2irit: memory allocation failed\n");
  144.             exit(1);
  145.         }
  146.  
  147.         strcpy(GGroups[GGnum].name,gname);
  148.         GGroups[GGnum].id = GGnum;
  149.         GGroups[GGnum].fnum = 0;
  150.         GGroups[GGnum].FirstFace = GFnum;
  151.  
  152.         /* GPrevGroup = GCurrGroup; */
  153.         GCurrGroup = GGnum;
  154.         GGnum++;
  155.         }
  156.         else {
  157.         /* The found group is already set. */
  158.         if (GGroups[i].FirstFace == 0)
  159.             GGroups[i].FirstFace = GFnum;
  160.  
  161.         /* GPrevGroup = GCurrGroup; */
  162.         GCurrGroup = i;
  163.         }
  164.     }
  165.     else if (!strcmp(FirstWord,"v")) {
  166.         if((GVnum - 99) % 100 == 0)
  167.         Add100(&GVerts, GVnum);
  168.  
  169.         {
  170.         char t0[30];
  171.         char t1[30];
  172.         char t2[30];
  173.         sscanf(Line,"v %s %s %s", t0, t1, t2);
  174.         GVerts[GVnum][0] = atof(t0);
  175.         GVerts[GVnum][1] = atof(t1);
  176.         GVerts[GVnum][2] = atof(t2);
  177.         }
  178.         GVnum ++;
  179.     }
  180.     else if (!strcmp(FirstWord,"vn")) {
  181.         if((GVnnum - 99) % 100 == 0)
  182.             Add100(&GNverts,GVnnum);
  183.  
  184.         {
  185.         char t0[30];
  186.         char t1[30];
  187.         char t2[30];
  188.         sscanf(Line,"v %s %s %s", t0, t1, t2);
  189.         GNverts[GVnnum][0] = atof(t0);
  190.         GNverts[GVnnum][1] = atof(t1);
  191.         GNverts[GVnnum][2] = atof(t2);
  192.         }
  193.         GVnnum ++;
  194.     }
  195.     else if (!strcmp(FirstWord,"f") || !strcmp(FirstWord,"fo")) {
  196.         char * word;
  197.         char tmpLine[200];
  198.  
  199.         /* 0 curr group need not to close prev. */
  200.         if (!GCurrGroupOpen) {
  201.         fprintf(stdout,"[OBJECT   %s\n ",
  202.             GGroups[GCurrGroup].name);
  203.         GPrevGroup = GCurrGroup;
  204.         GCurrGroupOpen = 1;
  205.         }
  206.  
  207.         strcpy(tmpLine, Line);
  208.         fprintf(stdout,"\t[POLYGON %d\n",strwrd(tmpLine) - 1);
  209.         /* strwrd spoils the Line. */
  210.  
  211.         strtok(Line," \t\n");
  212.         /* skip over 'f'. */
  213.         while ((word = strtok(NULL," \t\n")) != NULL) {
  214.         int j, k;
  215.         if(strchr(word,'/')) {
  216.             sscanf(word,"%d//%d",&j,&k);
  217.             printf("\t\t[[NORMAL %f %f %f] %f %f %f]\n",
  218.                GNverts[k-1][0],
  219.                GNverts[k-1][1],
  220.                GNverts[k-1][2],
  221.                GVerts[j-1][0],
  222.                GVerts[j-1][1],
  223.                GVerts[j-1][2]);
  224.         }
  225.         else {
  226.             sscanf(word,"%d",&j);
  227.             printf("\t\t[%f %f %f]\n",
  228.                GVerts[j-1][0],
  229.                GVerts[j-1][1],
  230.                GVerts[j-1][2]);
  231.         }
  232.         }
  233.  
  234.         printf("\t]\n");
  235.     }
  236.     }
  237.  
  238.     if (GCurrGroupOpen)
  239.         fprintf(stdout,"] /* OBJECT %s */\n", GGroups[GCurrGroup].name);
  240. }
  241.  
  242. static int strwrd(char * Line)
  243. {
  244.     int i = 1;
  245.  
  246.     if (!strtok(Line," \t\n"))
  247.         return(0);
  248.     while (strtok(NULL," \t\n") != NULL) 
  249.         i++;
  250.  
  251.     return i;
  252. }
  253.